package com.taxshare.seller.util.sst;

import com.taxshare.seller.constant.CodeConstant;
import com.taxshare.seller.entity.EquipmentEntity;
import com.taxshare.seller.entity.SellerRedInvoiceDetailEntity;
import com.taxshare.seller.entity.SellerRedInvoiceEntity;
import com.taxshare.seller.entity.SyncRedTicketXmlEntity;
import com.taxshare.tools.util.AreaCodeUtil;
import com.taxshare.tools.util.DateUtil;
import com.taxshare.tools.util.UUIDUitl;
import org.apache.commons.collections.MapUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author: liupei
 * @Date: 2019-01-17 9:59
 * @Description: 拼装接口报文
 */
public class IntfXmlUtil {
    private IntfXmlUtil(){
        
    }
    private static String cdateS = "<![CDATA[";
    private static String cdateE = "]]>";

    private static String transInvoiceType(String invoiceType) {
        if ("004".equals(invoiceType)) {
            invoiceType = "0";
        }
        if ("007".equals(invoiceType)) {
            invoiceType = "2";
        }
        if ("026".equals(invoiceType)) {
            invoiceType = "51";
        }
        return invoiceType;
    }


    /**
     * 同步红字信息表
     *
     * @param equipmentMode
     * @param entity
     * @return
     */
    public static String syncRedTicketInfo(String equipmentMode, SyncRedTicketXmlEntity entity) {
        StringBuilder sb = new StringBuilder();
        // 金税盘
        if (CodeConstant.EQUIPMENT_JSP.equals(equipmentMode)) {
            sb.append(CodeConstant.XML_HEADER);
            sb.append("<business comment=\"红字信息表下载\" id=\"HZTZDXZ\" version=\"2.0\">");
            sb.append(CodeConstant.HEAD);
            sb.append(CodeConstant.APPID + entity.getAppId() + CodeConstant.APPID_E);
            sb.append(CodeConstant.SID + entity.getSid() + CodeConstant.SID_E);
            sb.append(CodeConstant.HEAD_E);
            sb.append(CodeConstant.BODY_YYLXDM);
            sb.append(CodeConstant.INPUT);
            sb.append(CodeConstant.FPLXDM_0);
            sb.append("<xxbbh>" + entity.getXxbbh() + "</xxbbh>");
            sb.append("<sqqsrq>" + entity.getSqqsrq() + "</sqqsrq>");
            sb.append("<sqzzrq>" + entity.getSqzzrq() + "</sqzzrq>");
            sb.append(CodeConstant.INPUT_E);
            sb.append(CodeConstant.BODY_E);
            sb.append(CodeConstant.BUSINESS);
        } else if (CodeConstant.EQUIPMENT_SKP.equals(equipmentMode)) {
            // 税控盘
            sb.append(CodeConstant.XML_HEADER);
            sb.append("<business comment=\"红字信息表下载\" id=\"HZTZDXZ\" version=\"2.0\">");
            sb.append(CodeConstant.HEAD);
            sb.append(CodeConstant.APPID + entity.getAppId() + CodeConstant.APPID_E);
            sb.append(CodeConstant.SID + entity.getSid() + CodeConstant.SID_E);
            sb.append(CodeConstant.HEAD_E);
            sb.append(CodeConstant.BODY_YYLXDM);
            sb.append(CodeConstant.INPUT);
            sb.append(CodeConstant.SERIP + entity.getSvrIp() + CodeConstant.SERIP_E);
            sb.append(CodeConstant.SERPORT + entity.getSvrPort() + CodeConstant.SERPORT_E);
            sb.append(CodeConstant.FPLXDM_004);
            sb.append("<xxbbh>" + entity.getXxbbh() + "</xxbbh>");
            sb.append("<sqqsrq>" + entity.getSqqsrq() + "</sqqsrq>");
            sb.append("<sqzzrq>" + entity.getSqzzrq() + "</sqzzrq>");
            sb.append(CodeConstant.INPUT_E);
            sb.append(CodeConstant.BODY_E);
            sb.append(CodeConstant.BUSINESS);
        }
        return sb.toString();
    }

    /**
     * 申请红字信息表
     *
     * @return
     */
    public static String uploadRedTicketInfo(EquipmentEntity modeEntity, SellerRedInvoiceEntity entity,
                                             List<SellerRedInvoiceDetailEntity> details) {
        StringBuilder sb = new StringBuilder();
        String equipmentMode = modeEntity.getEquipmentMode();
        // 金税盘
        if ("0".equals(equipmentMode)) {
            sb.append(CodeConstant.XMLHEADER_U);
            sb.append(CodeConstant.HZTZDSC);
            sb.append(CodeConstant.HEAD);
            sb.append(CodeConstant.APPID + modeEntity.getAppid() + CodeConstant.APPID_E);
            sb.append(CodeConstant.SID + UUIDUitl.UUID32() + CodeConstant.SID_E);
            sb.append(CodeConstant.HEAD_E);
            sb.append(CodeConstant.BODY_YYLXDM);
            sb.append(CodeConstant.INPUT);
            sb.append(CodeConstant.FPLXDM_0);
            sb.append(CodeConstant.RED_REQ_BILL);
            sb.append(CodeConstant.REQ_NSRSBH + entity.getSellerTaxNo() + CodeConstant.REQ_NSRSBH_E);
            sb.append("<Kpjh>0</Kpjh>");
            sb.append(CodeConstant.SBBH + modeEntity.getPanNo() + CodeConstant.SBBH_E);
            sb.append(CodeConstant.REQ_BILL_NO);
            sb.append(CodeConstant.BILL_TYPE);
            sb.append(CodeConstant.TYPE_CODE + entity.getInvoiceCode() + CodeConstant.TYPE_CODE_E);
            sb.append(CodeConstant.INV_NO + entity.getInvoiceNo() + CodeConstant.INV_NO_E);
            sb.append(CodeConstant.SZLB);
            sb.append(CodeConstant.IS_MUTI_RATE);
            sb.append(CodeConstant.DATE + DateUtil.getCurrentYmd() + CodeConstant.DATE_E);
            sb.append(CodeConstant.BUY_NAME + entity.getPurchaserName() + CodeConstant.BUY_NAME_E);
            sb.append(CodeConstant.BUY_TAX_CODE + entity.getPurchaserTaxNo() + CodeConstant.BUY_TAX_CODE_E);
            sb.append(CodeConstant.SELLER_NAME + entity.getSellerName() + CodeConstant.SELLER_NAME_E);
            sb.append(CodeConstant.SELLER_TAX_NO_X + entity.getSellerTaxNo() + CodeConstant.SELLER_TAX_NO_X_E);
            sb.append(CodeConstant.AMOUNT + entity.getTotalSum() + CodeConstant.AMOUNT_E);
            sb.append(CodeConstant.TAX_RATE + entity.getTaxRate() + CodeConstant.TAX_RATE_E);
            sb.append(CodeConstant.TAX + entity.getTotalTax() + CodeConstant.TAX_E);
            sb.append("<ReqMemo>00000001100</ReqMemo>");
            sb.append(CodeConstant.SPBMBBH);
            sb.append(CodeConstant.SLBZ);
            sb.append(CodeConstant.RED_INV_REQ_BILL_MX);
            // 拼装明细
            buildUploadRedTicketDetail(sb, details);
            sb.append(CodeConstant.RED_INV_REQ_BILL_MX_E);
            sb.append(CodeConstant.RED_INV_REQ_BILL);
            sb.append(CodeConstant.INPUT_E);
            sb.append(CodeConstant.BODY_E);
            sb.append(CodeConstant.BUSINESS);
        } else if ("1".equals(equipmentMode)) {
            Map ipAndPort = AreaCodeUtil.getAreaIpIpPort(entity.getInvoiceCode());
            String fpssqyip = MapUtils.getString(ipAndPort, "fpssqyip", "");
            String fpssqydk = MapUtils.getString(ipAndPort, "fpssqydk", "");
            // 税控盘
            sb.append("<?xml version='1.0'encoding='utf-8'?>");
            sb.append(CodeConstant.HZTZDSC);
            sb.append(CodeConstant.HEAD);
            sb.append(CodeConstant.BODY_YYLXDM);
            sb.append(CodeConstant.INPUT);
            sb.append(CodeConstant.SERIP + fpssqyip + CodeConstant.SERIP_E);
            sb.append(CodeConstant.SERPORT + fpssqydk + CodeConstant.SERPORT_E);
            sb.append(CodeConstant.FPLXDM_004);
            sb.append(CodeConstant.RED_REQ_BILL);
            sb.append(CodeConstant.REQ_NSRSBH + entity.getSellerTaxNo() + CodeConstant.REQ_NSRSBH_E);
            sb.append(CodeConstant.SBBH + modeEntity.getPanNo() + CodeConstant.SBBH_E);
            sb.append(CodeConstant.REQ_BILL_NO);
            sb.append(CodeConstant.BILL_TYPE);
            sb.append(CodeConstant.TYPE_CODE + entity.getInvoiceCode() + CodeConstant.TYPE_CODE_E);
            sb.append(CodeConstant.INV_NO + entity.getInvoiceNo() + CodeConstant.INV_NO_E);
            sb.append(CodeConstant.SZLB);
            sb.append(CodeConstant.IS_MUTI_RATE);
            sb.append(CodeConstant.DATE + DateUtil.getCurrentYmd() + CodeConstant.DATE_E);
            sb.append(CodeConstant.BUY_NAME + entity.getPurchaserName() + CodeConstant.BUY_NAME_E);
            sb.append(CodeConstant.BUY_TAX_CODE + entity.getPurchaserTaxNo() + CodeConstant.BUY_TAX_CODE_E);
            sb.append(CodeConstant.SELLER_NAME + entity.getSellerName() + CodeConstant.SELLER_NAME_E);
            sb.append(CodeConstant.SELLER_TAX_NO_X + entity.getSellerTaxNo() + CodeConstant.SELLER_TAX_NO_X_E);
            sb.append(CodeConstant.AMOUNT + entity.getTotalSum() + CodeConstant.AMOUNT_E);
            sb.append(CodeConstant.TAX_RATE + entity.getTaxRate() + CodeConstant.TAX_RATE_E);
            sb.append(CodeConstant.TAX + entity.getTotalTax() + CodeConstant.TAX_E);
            sb.append("<ReqMemo>2</ReqMemo> ");
            sb.append(CodeConstant.SPBMBBH);
            sb.append(CodeConstant.SLBZ);

            // 拼装明细
            sb.append(CodeConstant.RED_INV_REQ_BILL_MX);
            buildUploadRedTicketDetail(sb, details);
            sb.append(CodeConstant.RED_INV_REQ_BILL_MX_E);
            sb.append(CodeConstant.RED_INV_REQ_BILL);
            sb.append(CodeConstant.INPUT_E);
            sb.append(CodeConstant.BODY_E);
            sb.append(CodeConstant.BUSINESS);
        }

        return sb.toString();
    }

    private static void buildUploadRedTicketDetail(StringBuilder sb, List<SellerRedInvoiceDetailEntity> details) {
        for (int i = 0; i < details.size(); i++) {
            SellerRedInvoiceDetailEntity detail = details.get(i);
            sb.append("<GoodsMx>");
            sb.append("<GoodsName>" + detail.getGoodsName() + "</GoodsName>");
            sb.append("<GoodsUnit>" + detail.getUnit() + "</GoodsUnit>");
            sb.append("<GoodsPrice>" + detail.getUnitPrice() + "</GoodsPrice>");
            sb.append("<GoodsTaxRate>" + detail.getTaxRate() + "</GoodsTaxRate>");
            sb.append("<GoodsGgxh>" + detail.getGoodType() + "</GoodsGgxh>");
            sb.append("<GoodsNum>" + detail.getGoodNumber() + "</GoodsNum>");
            sb.append("<GoodsJE>" + detail.getGoodsSum() + "</GoodsJE>");
            sb.append("<GoodsSE>" + detail.getGoodTax() + "</GoodsSE>");
            sb.append("<HS_BZ>N</HS_BZ>");
            sb.append(CodeConstant.SPBM + detail.getTaxCode() + CodeConstant.SPBM_E);
            sb.append("<QYSPBM></QYSPBM>");
            sb.append("<SYYHZCBZ>" + detail.getPreferentialMark() + "</SYYHZCBZ>");
            sb.append("<YHZC>" + detail.getPreferentialName() + "</YHZC>");
            sb.append("<LSLBZ>" + detail.getTaxRateMark() + "</LSLBZ>");
            sb.append("</GoodsMx>");
        }
    }

    public static String getSlv(String sl){
        if ("0.1".equals(sl)) {
            sl = "0.10";
        }
        return sl;
    }
}
